%% Some basic 'if' statement examples

clc
if 1==1 % This is true so it will run the code between 'if' and 'end'
    fprintf('Its True\n')
end

clc
% We can have more complicated 'if' statements. 
N=3
if N==5
    fprintf('Its True that N=5\n')
elseif N==4
    fprintf('Its True that N=4\n')
else % This runs if none of the 'if' or 'ifelse' are true
    fprintf('Its Not True!\n')
end

%%  Finding Prime Numbers

N=10 %The number of primes we want to find
Cnt=0; %Number we have found so far in the loop 
CurrentNum=2 %Our starting number (primes are >1)
PrimeArray=[] %This is where we will store the results
% The '[]' means an empty array. 

while Cnt<N %Keep going until we find N primes
    FoundDivisor=0 % We will count the number of Divisors we find
    for nn=2:sqrt(CurrentNum) % Check numbers that could be Divisors
        FoundDivisor=FoundDivisor+(mod(CurrentNum,nn)==0)
        %Note the parenthesis for order of operation!
    end
    
    if FoundDivisor==0 %There were no divisors, so a prime
       Cnt=Cnt+1; %Increment the number we found
       PrimeArray(Cnt)=CurrentNum % Write the number under test into array
    end
    CurrentNum=CurrentNum+1; %Increment to try the next highest integer
    
end
PrimeArray % display array in command window

%% Functions

% Let's wrap up the above into a function. 
% Editor Tab -> New -> Function
% Change function name to 'findPrimes' and save as 'findPrimes.m'
% See inside function

MM=20;
OutArray=findPrimes(MM)

help findPrimes

%% Another simple function

% Editor Tab -> New -> Function
% Change function name to 'addNums' and save as 'addNums.m'
% See inside function

A=5
B=10

[AplusB] = addNums(A,B)

% You should be able to create a new simple function
% and get it working in ~ 1 min. 

%Try setting a breakpoint inside the addNums function. 

%Important: Don't name your function or variables the 
% same name as an exising MATLAB function. 







